package com.example.sqliteapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.sqliteapplication.bean.Student;
import com.example.sqliteapplication.utils.DBUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.btn_insert)
    Button btnInsert;
    @BindView(R.id.btn_serach_all)
    Button btnSerachAll;
    @BindView(R.id.btn_delete)
    Button btnDelete;
    @BindView(R.id.btn_update)
    Button btnUpdate;
    //private SQLiteDatabase db;
    private String tag = "MyDBHelper";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
//        MyDBHelper helper=new MyDBHelper(getApplicationContext());
//        db = helper.getReadableDatabase();
        Button btnInsert = (Button) findViewById(R.id.btn_insert);
        Button btnAll = (Button) findViewById(R.id.btn_serach_all);
        btnInsert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DBUtils.updateName(getApplicationContext(), "tom", "kate");
//                DBUtils.deleteByName(getApplicationContext(),"sam");
                // DBUtils.insert(getApplicationContext(),new Object[]{"1003","sam","1111111111"});
//                db.execSQL("insert into info(_id,name) values(?,?)",new Object[]{"1001","tom"});
//                db.execSQL("insert into info(_id,name) values(?,?)",new Object[]{"1002","jerry"});
//                db.execSQL("insert into info(_id,name) values(?,?)",new Object[]{"1003","john"});
            }
        });
        btnAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Cursor cursor=db.rawQuery("select * from info",null);
//                while(cursor.moveToNext()){
//                    String id=cursor.getString(0);
//                    String name=cursor.getString(1);
//                    Log.i(tag, "data="+id+"--"+name);
//                }
                ArrayList<Student> students = DBUtils.findall(getApplicationContext());
                for (Student s : students) {
                    Log.i("MyDBHelper", "onClick: " + s);

                }

            }
        });
    }

    @OnClick({R.id.btn_insert, R.id.btn_serach_all, R.id.btn_delete, R.id.btn_update})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_insert:
                break;
            case R.id.btn_serach_all:
                break;
            case R.id.btn_delete:
                break;
            case R.id.btn_update:
                break;
        }
    }
}
